package com.smfx.yxt.controller.sys;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smfx.yxt.common.Const;
import com.smfx.yxt.common.RspResult;
import com.smfx.yxt.common.enums.Status;
import com.smfx.yxt.model.Grid;
import com.smfx.yxt.model.GridBase;
import com.smfx.yxt.model.OrganizeDepart;
import com.smfx.yxt.model.OrganizeGroup;
import com.smfx.yxt.model.TeacherInfo;
import com.smfx.yxt.page.OrderBy;
import com.smfx.yxt.page.Page;
import com.smfx.yxt.reqVo.TeacherReqVO;
import com.smfx.yxt.service.IOrganizeGroupService;
import com.smfx.yxt.service.ITeacherService;
import com.smfx.yxt.utils.BeanUtils;
import com.smfx.yxt.utils.StringUtil;
import com.smfx.yxt.view.OrganizeGroupView;
import com.smfx.yxt.view.TeacherInfoView;
import com.smfx.yxt.wechat.message.WechatEwm;
import com.smfx.yxt.wechat.model.WechatQrcodeRsp;
import com.smfx.yxt.wechat.utils.MessageUtil;
import com.smfx.yxt.wechat.utils.WechatGlobalMap;
import com.smfx.yxt.wechat.utils.WechatUtils;

@RequestMapping("organize")
@Controller
public class OrganizeController {
	
	private Logger logger = LoggerFactory.getLogger(OrganizeController.class);
	
	@Autowired
	private ITeacherService teacherService;
	@Autowired
	private IOrganizeGroupService organizeGroupService;

	@RequestMapping("teacher/manage")
	public String organizeTeacherManage() {
		return "sys/teacherManage";
	}
	
	@RequestMapping("workgroup/manage")
	public String organizeWorkgroupManage() {
		return "sys/workgroupManage";
	}
	
	@RequestMapping("workgroup/manage/query")
	@ResponseBody Object organizeWorkgroupManageQuery(GridBase gridBase) {
		try {
			Page<OrganizeGroup> page = new Page<OrganizeGroup>();
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("ID"));
			sorts.add(new OrderBy("WEIGHT"));
			page.setSorts(sorts);
			page.setPageNo(gridBase.getPage());
			page.setPageSize(gridBase.getRows());
			Grid grid = new Grid();
			List<OrganizeGroup> list = organizeGroupService.selectListByPage(page);
			List<OrganizeGroupView> listView = BeanUtils.copyAs(list, OrganizeGroupView.class);
			grid.setRows(listView);
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			logger.error("----organizeWorkgroupManageQuery error={}", e);
		}
		return null;
	}
	
	@RequestMapping("teacher/manage/query")
	@ResponseBody Object organizeTeacherQuery(TeacherReqVO vo){
		try {
			Page<TeacherInfo> page = new Page<TeacherInfo>();
			Grid grid = new Grid();
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("ID"));
			page.setSorts(sorts);
			page.setParams(vo);
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			List<TeacherInfo> list = teacherService.selectByPage(page);
			List<TeacherInfoView> listView = BeanUtils.copyAs(list, TeacherInfoView.class);
			grid.setRows(listView);
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			logger.error("---organizeTeacherManage error={}", e);
		}
		return null;
	}
	
	@RequestMapping("teacher/manage/update")
	@ResponseBody Object teacherManageUpdate(TeacherInfo record) {
		try {
			if(StringUtils.isNotBlank(record.getPosition())) {
				String[] strings = record.getPosition().split(",");
				Map<String, OrganizeDepart> map = (Map<String, OrganizeDepart>) WechatGlobalMap.get(Const.CACHE_ORGANIZE_DEPART).getValue();
				String value = "";
				for(int i = 0; i < strings.length; i++) {
					value += map.get(strings[i]).getParentId();
					if((i+1) != strings.length) value += ",";
				}
				record.setDepart(StringUtil.delRepeatStr(value));
			}
			teacherService.updateById(record);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("---teacherManageUpdate error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("teacher/wechat/unbind")
	@ResponseBody Object wechatUnbind(Integer teacherId) {
		try {
			TeacherInfo teacher = new TeacherInfo();
			teacher.setId(teacherId);
			teacher.setOpenid2(null);
			teacher.setHeadImgUrl(null);
			teacherService.updateUnbindWechat(teacher);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("---wechatUnbind error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("teacher/create/bindEwm")
	@ResponseBody Object createBindEwm(Integer teacherId) {
		try {
			WechatQrcodeRsp rsp = WechatUtils.getQrcode(MessageUtil.QR_STR_SCENE, 
					WechatEwm.INVITE_BIND_TEA_WEM.getCode(), String.valueOf(teacherId));
			if(rsp != null) {
				return new RspResult(Status.SUCCESS, rsp.getQrcode());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("workgroup/manage/update")
	@ResponseBody Object workgroupManageUpdate(OrganizeGroup record) {
		try {
			organizeGroupService.updateByPrimaryKeySelective(record);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("---workgroupManageUpdate error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
}
